// pages/login/login.js 账号密码登陆页面
import { request } from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: true, //控制按钮是否可点击
    username: "",    //用户名
    password: "",     //密码
  },
  /**
   * 两个协议
   */
  consumer(e) {
    wx.navigateTo({
      url: '../webView/consumer/consumer',
      success: res => {
      }
    })
  },
  privacy(e) {
    wx.navigateTo({
      url: '../webView/privacy/privacy',
      success: res => {
      }
    })
  },
  /**
   * 登陆
   * sessionid
   */
  formSubmit(e) {
    const that = this;
    request("POST", "/api/auth/login/", e.detail.value, "", true)
      .then(
        (res) => {
          if (res.statusCode === 200) {
            if (res.data.code === 200) {
              const csrf = res.cookies[0] || ""
              const sessionId = res.cookies[1] || ""
              const a = csrf.split(";")[0]
              const b = a.split("=")[1]
              const b_name = a.split("=")[0]
              const c = sessionId.split(";")[0]
              const d = c.split("=")[1]
              const d_name = c.split("=")[0]
              if (b_name === "csrftoken") {
                wx.setStorageSync('csrf', b)
              } else {
                wx.setStorageSync('csrf', d)
              }
              wx.setStorageSync(b_name, b_name + "=" + b)
              wx.setStorageSync(d_name, d_name + "=" + d)
              wx.setStorageSync('userInfo', res.data.userInfo)
                
               
              if(res.data.userInfo.org_list){
                const param = {
                  username:that.data.username,
                  org_list:res.data.userInfo.org_list
                }
                  wx.navigateTo({
                    url: `/pages/components/OrgChoice/orgChoic?param=${JSON.stringify(param)}`,
                  })
              }else{
                wx.setStorageSync('userInfo', res.data.userInfo)
                wx.switchTab({
                  url: '../tabBar/search/search',
                })
              }
                
                
              
            } else if (res.data.code === 211) {
              wx.showToast({
                title: '账号或密码错误',
                icon: 'none',
                duration: 2000
              })
              that.setData({
                password: '',
                disabled: true
              })
            } else if (res.data.code === 213) {
              wx.showToast({
                title: '用户账户已作废',
                icon: 'none',
                duration: 2000
              })
              that.setData({
                username: '',
                password: '',
                disabled: true
              })
            } else if (res.data.code === 212) {
              wx.showToast({
                title: '账号未注册',
                icon: 'none',
                duration: 2000
              })
              that.setData({
                username: '',
                password: '',
                disabled: true
              })
            }

          } else if (res.statusCode === 404) {

            wx.showToast({
              title: '服务器出错请稍后再试',
              icon: 'none',
              duration: 2000
            })
          } else if (res.statusCode === 500) {
            wx.showToast({
              title: '服务器出错请稍后再试',
              icon: 'none',
              duration: 2000
            })
          } else {


          }
        }

      )
  },
  /**
   * 检测按钮是否可用
   * 输入事件
   */
  setuserVal(e) {
    
    this.setData({
      username: e.detail.value
    })
    if (this.data.username.length >= 1 && this.data.password.length >= 1) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
  setpassVal(e) {
    this.setData({
      password: e.detail.value
    })
    if (this.data.username.length >= 1 && this.data.password.length >= 1) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
})